package com.pan.admin.module.loanUser.service.impl;

import com.gitee.apanlh.util.base.StringUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.annotation.DataScope;
import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.module.channel.dao.LoanChannelDao;
import com.pan.admin.module.loanProduct.dao.LoanProductDao;
import com.pan.admin.module.loanProduct.dao.LoanProductPlanDao;
import com.pan.admin.module.loanUser.dao.LoanUserRegisterMapper;
import com.pan.admin.module.loanUser.entity.LoanUserRegister;
import com.pan.admin.module.loanUser.entity.LoanUserRegisterProduct;
import com.pan.admin.module.loanUser.entity.qo.LoanUserRegisterProductQo;
import com.pan.admin.module.loanUser.entity.qo.LoanUserRegisterQO;
import com.pan.admin.module.loanUser.service.LoanUserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 用户注册Service业务层处理
 * @Author: ghb
 * @Date: 2023/9/7 16:53
 */
@Service
public class LoanUserRegisterServiceImpl implements LoanUserRegisterService {

    @Autowired
    private LoanUserRegisterMapper loanUserRegisterMapper;
    @Autowired
    private LoanChannelDao loanChannelDao;
    @Autowired
    private LoanProductDao loanProductDao;
    @Autowired
    private LoanProductPlanDao loanProductPlanDao;

    @Override
    @Transactional(readOnly = true)
    public List<LoanUserRegister> list(LoanUserRegisterQO loanUserRegisterQO) {
        if (ValidParam.isNotEmpty(loanUserRegisterQO.getMultiplePhone())) {
            //  去除空格
            String multiplePhone = loanUserRegisterQO.getMultiplePhone().replace(" ", "");
            //  将多个手机号码转换成手机号码数组
            List<String> phoneList = StringUtils.splitToList(multiplePhone, ",");
            loanUserRegisterQO.setPhoneList(phoneList);
        }

        List<LoanUserRegister> list = this.loanUserRegisterMapper.list(loanUserRegisterQO);
        list.forEach(item -> {
            Long loanChannelId = item.getLoanChannelId();
            if (loanChannelId == null) {
                return;
            }
            String loanChannelName = this.loanChannelDao.findLoanChannelNameByLoanChannelId(loanChannelId);
            item.setLoanChannelName(loanChannelName);
        });

        list.forEach(item -> {
            Long loanProductId = item.getLoanProductId();
            if (loanProductId == null) {
                return;
            }
            String LoanProductName = this.loanProductDao.findLoanProductNameByLoanProductId(loanProductId);
            item.setLoanProductName(LoanProductName);
        });

        list.forEach(item -> {
            Long loanProductPlanId = item.getLoanProductPlanId();
            if (loanProductPlanId == null) {
                return;
            }
            String loanProductPlanName = this.loanProductPlanDao.findLoanProductPlanNameByLoanProductPlanId(loanProductPlanId);
            item.setLoanProductPlanName(loanProductPlanName);
        });
        return list;
    }

    /**
     * 查询用户注册
     *
     * @param id 用户注册主键
     * @return 用户注册
     */
    @Override
    public LoanUserRegister selectLoanUserRegisterById(Long id) {
        return this.loanUserRegisterMapper.getById(id);
    }

    /**
     * 查询用户注册列表
     *
     * @param loanUserRegister 用户注册
     * @return 用户注册
     */
    @Override
    @Transactional(readOnly = true)
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<LoanUserRegister> listCrm(LoanUserRegister loanUserRegister) {
        Integer productId = SecurityUtils.getLoginUser().getUser().getProductId();
        loanUserRegister.setLoanProductId(productId.longValue());
        return this.loanUserRegisterMapper.listCrm(loanUserRegister);
    }

    /**
     * 查询用户注册列表详情
     */
    @Override
    public List<LoanUserRegister> detailsChannelList(LoanUserRegister loanUserRegister) {
        List<LoanUserRegister> loanUserRegisters = this.loanUserRegisterMapper.detailsChannelList(loanUserRegister);
        List<LoanUserRegister> loanUserRegisters1 = LoanUserRegister.distinctChannelList(loanUserRegisters);
        return loanUserRegisters1;
    }

    /**
     * 查询用户注册列表机构详情
     */
    @Override
    public List<LoanUserRegisterProduct> listUserByProductId(LoanUserRegisterProductQo loanUserRegister) {
        Integer productId = SecurityUtils.getLoginUser().getUser().getProductId();
        loanUserRegister.setLoanProductId(productId);
        List<LoanUserRegisterProduct> list = this.loanUserRegisterMapper.listUserByProductId(loanUserRegister);
        return list;
    }


    /**
     * 新增用户注册
     *
     * @param loanUserRegister 用户注册
     * @return 结果
     */
    @Override
    public int insertLoanUserRegister(LoanUserRegister loanUserRegister) {
        loanUserRegister.setCreateTime(DateUtils.getNowDate());
        return this.loanUserRegisterMapper.insertLoanUserRegister(loanUserRegister);
    }

    /**
     * 修改用户注册
     *
     * @param loanUserRegister 用户注册
     * @return 结果
     */
    @Override
    public int updateLoanUserRegister(LoanUserRegister loanUserRegister) {
        loanUserRegister.setUpdateBy(SecurityUtils.getUsername());
        loanUserRegister.setUpdateTime(DateUtils.getNowDate());
        return this.loanUserRegisterMapper.updateLoanUserRegister(loanUserRegister);
    }

}
